package com.dexilaonian.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.dexilaonian.R;
import com.dexilaonian.adpter.NewsAdapter;
import com.dexilaonian.bean.NewsBean;
import com.dexilaonian.ui.BaseActivity;
import com.dexilaonian.utils.Constant;
import com.dexilaonian.utils.JsonUtil;
import com.dexilaonian.utils.Okhttp;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

public class NewsActivity extends BaseActivity {
    @Bind(R.id.back)
    ImageView back;
    @Bind(R.id.base_topText)
    TextView baseTopText;
    @Bind(R.id.base_choose)
    TextView baseChoose;
    @Bind(R.id.news_listview)
    ListView newsListview;
    @Bind(R.id.news_delete)
    Button newsDelete;

    private List<NewsBean> newList = new ArrayList<>();
    private NewsAdapter newsAdapter;
    private String aging_url = Constant.base_url + "/user/getMyPMessages.do";
    private String delete_url = Constant.base_url + "/msg/removeMessage.do";

    private int isShow;
    private Map<String, String> map = new HashMap<>();
    private String id1;

    @Override
    public int getLayoutID() {
        return R.layout.activity_news;
    }

    @Override
    public void initView() {

    }

    @Override
    protected void initData() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
        baseChoose.setVisibility(View.VISIBLE);
        baseTopText.setText("消息");
        initList();
    }

    private void init() {
        baseChoose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isShow == 0) {
                    baseChoose.setText("取消");
                    newsDelete.setVisibility(View.VISIBLE);
                    isShow = 1;
                    if (newsAdapter != null) {
                        newsAdapter.showCheckbox();
                    }
                } else {
                    baseChoose.setText("选择");
                    newsDelete.setVisibility(View.GONE);
                    isShow = 0;
                    if (newsAdapter != null) {
                        newsAdapter.hideCheckbox();
                    }
                }
            }
        });
        if (newsAdapter != null) {
            newsAdapter.setCheckboxListener(new NewsAdapter.OncheckboxListener() {
                @Override
                public void oncheckboxListener(boolean isSelct, int postion) {
                    NewsBean newsBean = newList.get(postion);
                    newsBean.setSelect(isSelct);
                    newList.set(postion, newsBean);
                }
            });
        }
    }

    private void initList() {
        showProgressBar(true);
        map.put("page", 1 + "");
        map.put("limit", 10 + "");
        Okhttp.postString(true, aging_url, map, new Okhttp.CallBac() {

            @Override
            public void onError(Call call, Exception e, String state, int id) {
                showProgressBar(false);
            }

            @Override
            public void onResponse(String response, int id) {
                showProgressBar(false);
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    int status = Integer.parseInt(jsonObject.optString("status"));
                    if (status == 0) {
                        String pMessages = jsonObject.optString("pMessages");
                        JSONObject object1 = new JSONObject(pMessages);
                        String items = object1.optString("items");
                        ArrayList<NewsBean> newsBeen = JsonUtil.json2beans(items, NewsBean.class);
                        for (int i = 0; i < newsBeen.size(); i++) {
                            id1 = newsBeen.get(i).getId();
                        }
                        newList.clear();
                        newList.addAll(newsBeen);
                        isShow = 0;
                        newsAdapter = new NewsAdapter(NewsActivity.this, newList);
                        newsListview.setAdapter(newsAdapter);
                        newsAdapter.notifyDataSetChanged();
                        init();
                        initBoxBeanList();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onNoNetwork(String state) {
                showProgressBar(false);
            }
        });
    }

    private void initBoxBeanList() {
        newsDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                initDelete();
            }
        });

    }

    private void initDelete() {
        showProgressBar(true);
        Map<String, String> map = new HashMap<>();
        map.put("messageID", id1);
        map.put("type", "1");
         Okhttp.postString(true, delete_url, map, new Okhttp.CallBac() {
        @Override
        public void onError(Call call, Exception e, String state, int id) {
            showProgressBar(false);
        }

        @Override
        public void onResponse(String response, int id) {
            showProgressBar(false);
            Iterator<NewsBean> iterator = newList.iterator();
            while (iterator.hasNext()) {
                NewsBean newsBean = iterator.next();
                if (newsBean.isSelect()) {
                    iterator.remove();
                }
            }
            newsAdapter.notifyDataSetChanged();
        }

        @Override
        public void onNoNetwork(String state) {
            showProgressBar(false);
        }
    });
    }

    /**
     * 点击加载数据，重新初始化
     */
    public void loadData(View v) {
        initBoxBeanList();
        newsAdapter.notifyDataSetChanged();
    }
    /**
     * 单项中选中状态改变时，由adpater回调
     */
    public void checkedStateChange(int position, boolean isChecked) {
        newsAdapter.notifyDataSetChanged();
    }

    @OnClick({R.id.back,R.id.base_topText})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back:
            case R.id.base_topText:
                finish();
                break;
        }
    }

}
